package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;

import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.PermissionCommand;
import com.cskaoyan.wordmemorize.request.RoleCommand;

import com.cskaoyan.wordmemorize.request.RolePermissionCommand;
import com.cskaoyan.wordmemorize.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin")
public class RolePermissionController {
    @Autowired
    RolePermissionService rolePermissionService;

    @PostMapping("role")
    public Result addRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.addRole(roleCommand);
        return Result.ok(null);
    }
    @PutMapping("role")
    public Result updateRole(@RequestBody RoleCommand roleCommand) {
        rolePermissionService.updateRole(roleCommand);
        return Result.ok(null);
    }

    @PutMapping("/role/permission")
    public Result updateRolePermission(@RequestBody RolePermissionCommand rolePermissionCommand) {
        rolePermissionService.updateRolePermission(rolePermissionCommand);
        return Result.ok(null);
    }
    @GetMapping("/role/list")
    public Result<PageDTO<RoleDTO>> getRoleList(PageRequest pageRequest) {
        PageDTO<RoleDTO> pageDTO=rolePermissionService.getRoleList(pageRequest);
        return Result.ok(pageDTO);
    }
    @DeleteMapping("role/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId) {
        DeleteRoleOperateDTO deleteRoleOperateDTO=rolePermissionService.deleteRole(roleId);
        return Result.ok();
    }
    @GetMapping ("{roleId}/permission/list")
    public Result getRolePermissionList(@PathVariable("roleId") Long roleId) {
        RolePermissionDTO rolePermissionDTO=rolePermissionService.getRolePermissionList(roleId);
        return Result.ok(rolePermissionDTO);
    }
    @DeleteMapping("permission/{roleId}/{permissionId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId,@PathVariable("permissionId") Long permissionId) {
        DeleteRolePermissionOperateDTO deleteRolePermissionOperateDTO =rolePermissionService.deleteRolePermission(roleId,permissionId);
        return Result.ok();
    }
    @PostMapping("permission")
    public Result savePermissions(@RequestBody PermissionCommand command) {
        rolePermissionService.savePermissions(command);
        return Result.ok();
    }
    @GetMapping("permission/list")
    public Result<List<PermissionDTO>> getPermissions() {
        List<PermissionDTO> permissionDTOList=rolePermissionService.getPermissions();
        return Result.ok(permissionDTOList);
    }
    @PutMapping("permission")
    public Result updatePermission(@RequestBody PermissionCommand command) {
        rolePermissionService.updatePermission(command);
        return Result.ok();
    }
    ///admin/permission/{permissionId}
    //DeletePermissionOperateDTO deletePermission(Long permissionId) {
    //         return null;
    //     }
    @DeleteMapping ("permission/{permissionId}")
    public Result deletePermission(@PathVariable("permissionId") Long permissionId) {
        rolePermissionService.deletePermission(permissionId);
        return Result.ok();
    }
}
